package com.member.member;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.member.member.feign.HelloFeignService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 86131
 */
@SpringBootApplication
@EnableEurekaClient
@RestController
@EnableFeignClients
//@EnableCircuitBreaker
@EnableHystrix
@Slf4j
public class MemberApplication {
    @Autowired
    private HelloFeignService helloFeignService;

    public static void main(String[] args) {
        SpringApplication.run(MemberApplication.class, args);
    }



//    @HystrixCommand(commandKey = "testCommand", groupKey = "testGroup", threadPoolKey = "testThreadKey",
//            fallbackMethod = "hiConsumerFallBack", ignoreExceptions = {NullPointerException.class},
//            commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "7000")
//            },
//            threadPoolProperties = {
//                    @HystrixProperty(name = "coreSize", value = "100"),
//                    @HystrixProperty(name = "maxQueueSize", value = "200"),
//                    @HystrixProperty(name = "keepAliveTimeMinutes", value = "2"),
//                    @HystrixProperty(name = "queueSizeRejectionThreshold", value = "100"),
////                    @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"),
////                    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1440")
//            }
//    )


//@HystrixCommand(fallbackMethod = "hiConsumerFallBack",commandKey = "testCommand", groupKey = "testGroup", threadPoolKey = "testThreadKey",
//        threadPoolProperties = {
//                @HystrixProperty(name = "coreSize", value = "200"),
//                @HystrixProperty(name = "maximumSize", value = "250"),
//                @HystrixProperty(name = "maxQueueSize", value = "200"),
//                @HystrixProperty(name = "queueSizeRejectionThreshold", value = "200"),
//                @HystrixProperty(name = "allowMaximumSizeToDivergeFromCoreSize", value = "true") // 默认false, 如果不设置该参数maximumSize=coreSize
//        },
//        commandProperties = {
//                @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "10"),   //（出错百分比阈值，当达到此阈值后，开始短路。默认50%）
//                @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "20"),      // 在统计数据之前，必须在10秒内发出3个请求。  默认是20
//                @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000"), //（短路多久以后开始尝试是否恢复，默认5s）
//                @HystrixProperty(name = "execution.timeout.enabled", value = "true"),
//                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "7000"), //线程执行超时时间，默认为1s，应该可满足99.5%的请求
////                @HystrixProperty(name = "fallback.isolation.semaphore.maxConcurrentRequests", value = "25")    //执行fallback方法的semaphore数量
//        })
    @RequestMapping("cc")
    public List<String> test11() throws InterruptedException {
        List<SecurityProperties.User> users=new ArrayList<>();
        boolean a=true;
//        while (a){
//            SecurityProperties.User user=new SecurityProperties.User();
//            user.setName("妻孥上次u是");
//            user.setPassword("dede");
//            users.add(user);
//        }
        List<String> test = new ArrayList<>();
        test.add("1");
        test.add("2");
        test.add("3");
        test.add("4");
        return test;
    }

    public List<String> hiConsumerFallBack() throws InterruptedException {
        List<String> test = new ArrayList<>();
        test.add("服务降级了");
        return test;
    }


    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500"),
            // 滑动统计的桶数量
            /**
             * 设置一个rolling window被划分的数量，若numBuckets＝10，rolling window＝10000，
             *那么一个bucket的时间即1秒。必须符合rolling window % numberBuckets == 0。默认1
             */
            @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "10"),
            // 设置滑动窗口的统计时间。熔断器使用这个时间
            /** 设置统计的时间窗口值的，毫秒值。
             circuit break 的打开会根据1个rolling window的统计来计算。
             若rolling window被设为10000毫秒，则rolling window会被分成n个buckets，
             每个bucket包含success，failure，timeout，rejection的次数的统计信息。默认10000
             **/
            @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "10000")},
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "15"),
                    /**
                     * BlockingQueue的最大队列数，当设为-1，会使用SynchronousQueue，值为正时使用LinkedBlcokingQueue。
                     */
                    @HystrixProperty(name = "maxQueueSize", value = "15"),
                    /**
                     * 设置存活时间，单位分钟。如果coreSize小于maximumSize，那么该属性控制一个线程从实用完成到被释放的时间.
                     */
                    @HystrixProperty(name = "keepAliveTimeMinutes", value = "2"),
                    /**
                     * 设置队列拒绝的阈值,即使maxQueueSize还没有达到
                     */
                    @HystrixProperty(name = "queueSizeRejectionThreshold", value = "15"),
                    @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "10"),
                    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "10000")
            })
    @RequestMapping("dd")
    public List<String> test1() throws InterruptedException {
        List<String> test = helloFeignService.test1();
        log.info("获取到的信息为:{}",test.get(0).toString());
        return test;
    }








}
